/* api */
import { getPageElementsNumList } from '@shb-lib/version'
/* hooks */
import { useLoading } from '@hooks/useLoading'
/* vue */
import { computed, ref, Ref } from 'vue'
/* model */
import MsgModel from '@model/MsgModel'
import { GetAuthPageElementsNumListParams, VersionControlKeyEnum } from '@shb-lib/version'
/* util */
import { LogUtil } from '@shb-lib/core'
import { setVersionControlMapToWindow } from '@shb-lib/version'

/** 
 * @description 售后宝 版本控制
*/
function useVersionFetchShbVersionControl() {
  
  const versionControlList: Ref<VersionControlKeyEnum[]> = ref([])
  
  // 加载状态
  const { loading, setLoading, clearLoading } = useLoading()
  
  // 获取租户已购产品信息列表
  const fetchPageElementsNumList = (params: GetAuthPageElementsNumListParams) => {
    
    setLoading()
    
    return (
      getPageElementsNumList(params).then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          const _versionControlList = result?.data || []
          
          setVersionControlMapToWindow(_versionControlList)
          
          versionControlList.value = _versionControlList
          
        }
        
      })
      .catch(error => {
        LogUtil.error(error, fetchPageElementsNumList.name)
      })
      .finally(() => {
        clearLoading()
      })
    )
    
  }
  
  return {
    fetchPageElementsNumList,
    loading,
    versionControlList
  }
}

export {
  useVersionFetchShbVersionControl
}
